package com.ywjt.platformbusiness.classify.fragment;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.stx.xhb.xbanner.XBanner;
import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseVPFragment;
import com.ywjt.platformbusiness.classify.adapter.ClassifyDesignerAdapter;
import com.ywjt.platformbusiness.classify.adapter.ClassifyGoodsAdapter;
import com.ywjt.platformbusiness.classify.model.DesignerModel;
import com.ywjt.platformbusiness.constants.Constants;
import com.ywjt.platformbusiness.constants.UrlConstants;
import com.ywjt.platformbusiness.http.HttpRequest;
import com.ywjt.platformbusiness.http.HttpStringCallBack;
import com.ywjt.platformbusiness.util.JsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ClassifyDesignerFragment extends BaseVPFragment implements View.OnClickListener {
    private XBanner banner;
    private Context context;
    private List<Integer> banners;
    private TextView tvClassify;
    private SmartRefreshLayout refresh;
    private RecyclerView rcGoods;
    private ClassifyDesignerAdapter designerAdapter;

    public static ClassifyDesignerFragment newInstance(String type) {
        Bundle arguments = new Bundle();
        arguments.putString("type",type);
        ClassifyDesignerFragment fl = new ClassifyDesignerFragment();
        fl.setArguments(arguments);
        return fl;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.classify_fragment,container,false);
        initView(view);
        return view;
    }


    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {

    }


    public void initView(View view) {
        context = getActivity();
        rcGoods = view.findViewById(R.id.rcGoods);
        refresh=view.findViewById(R.id.refresh);
        tvClassify=view.findViewById(R.id.tvClassify);
        tvClassify.setText("专属设计师");
        banner = view.findViewById(R.id.banner);

        banners = new ArrayList<>();
        banners.add(R.mipmap.icon_classify_banner_img);
        banners.add(R.mipmap.icon_classify_banner_img);
        banners.add(R.mipmap.icon_classify_banner_img);


        banner.setData(R.layout.item_banner, banners, null);
        banner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object object, View view, int position) {
                RelativeLayout container = (RelativeLayout) view;
                ImageView ivBanner = container.findViewById(R.id.ivBanner);
                Glide.with(context).load(banners.get(position)).into(ivBanner);
            }
        });

        banner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, Object obj, View view, int position) {
            }
        });
        banner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        setView();
        getDesigner();
    }

    /**
     * 在fragment首次可见时回调，可用于加载数据，防止每次进入都重复加载数据
     */
    @Override
    protected void onFragmentFirstVisible() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

        }
    }

    public void setView(){
        refresh.finishRefreshWithNoMoreData();
        refresh.setEnableAutoLoadMore(true);//开启自动加载功能（非必须）
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getDesigner();
                    }
                }, 1000);
            }
        });
    }


    //获取有推荐设计师的
    private void getDesigner() {
        new HttpRequest(getContext()).doPost(UrlConstants.getShopListByExclusive, null,new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                try {
                    String data = result.toString();
                    //把请求得数据转换成json对象
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(data);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        refresh.finishRefresh();
                        Object obj = JsonUtils.fromJson(result.toString(), DesignerModel.class);
                        if (obj instanceof DesignerModel) {
                            DesignerModel model=(DesignerModel)obj;
                            if (model.getResult().size()>0){
                                designerAdapter = new ClassifyDesignerAdapter(model.getResult());
                                rcGoods.setLayoutManager(new GridLayoutManager(context, 3, RecyclerView.VERTICAL, false));
                                rcGoods.setAdapter(designerAdapter);
                            }
                        }
                    }else {
                        Toast.makeText(getContext(),message,Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                refresh.finishRefresh();
                Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
            }
        });
    }

}
